using System;
using System.Collections.Generic;
using Entities;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using BusinessLogic;

public partial class _Default : System.Web.UI.Page 
{
    public List<UserInfo> Usuarios {
        get {
            if (ViewState["usuarios"] != null)
                return (List<UserInfo>)ViewState["usuarios"];
            else
            {
                Users provider = new Users();
                List<UserInfo> list = provider.GetAllUsers();
                ViewState["usuarios"] = list;
                return list;
            }
        }
        set { ViewState["usuarios"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            GridPopulate();
    }

    public void GridPopulate() {
        FGView.DataSource = this.Usuarios;
        FGView.DataBind();
    }
    
    protected void FGView_OnCheckedChanged(object sender, Foxy.WebControls.RowCheckedEventArgs e)
    {
        object chkBox = sender;
    }

    protected void FGView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        FGView.EditIndex = e.NewEditIndex;
        GridPopulate();
    }
    protected void FGView_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        
    }
    protected void FGView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) 
    {
        FGView.EditIndex = -1;
        GridPopulate();
    }
    protected void FGView_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = ((GridView)sender).Rows[e.RowIndex];
        int idUser = Convert.ToInt32(row.Cells[1].Text);
        UserInfo userFinded = this.Usuarios.Find(userItem=>userItem.ID == idUser);

        string name = ((TextBox)row.FindControl("txtFirstName")).Text;
        if(name != userFinded.FirstName && name != string.Empty)
            userFinded.FirstName = name;
        
        FGView.EditIndex = -1;
        GridPopulate();
    }

    protected void FGView_RowDoubleClick(object sender, Foxy.WebControls.RowDoubleClickEventArgs e)
    {
        GridViewRow row = e.GridViewRow;
        int idUser = Convert.ToInt32(row.Cells[1].Text);
    }
}